//TryCatchFinallyTest.java
package com.ylaihui.exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;

public class TryCatchFinallyTest {
	@Test
	public void test1(){
		FileInputStream fis = null;
		try {
			File file = new File("hello1.txt");
			fis = new FileInputStream(file);
			
			int data = fis.read();
			while(data != -1){
				System.out.print((char)data);
				data = fis.read();
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fis != null)  //如果不加该语句，文件不存在时，会报空指针异常
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
